<template>
  <el-dialog title="分配角色" :visible="showDialog" @close="btnCancel">
    <el-checkbox-group v-model="checkList">
      <el-checkbox
        v-for="role in roleList"
        :key="role.id"
        :label="role.id"
      >
        {{ role.name }}
      </el-checkbox>
    </el-checkbox-group>

    <template #footer>
      <el-row type="flex" justify="center">
        <el-button size="small" type="primary" @click="btnOK">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    userId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      // 全部角色对象
      roleList: [],
      // 选中的角色 id 数组
      checkList: []
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async getRoleList() {
      const { rows } = await getRoleList()
      this.roleList = rows
    },
    // 这里是弹窗, 每当点击弹出的时候
    // 被点击的是谁,原来的角色应该默认选中
    async getCheckedRole(id) {
      // 根据被点击的员工取得员工信息
      const { roleIds } = await getUserDetailById(id)
      this.checkList = roleIds
    },
    async btnOK() {
      // 发请求
      await assignRoles({
        id: this.userId,
        roleIds: this.checkList
      })
      // 提示用户
      this.$message.success('分配角色成功')
      // 关闭弹窗
      this.$emit('update:showDialog', false)
    },
    btnCancel() {
      this.$emit('update:showDialog', false)
    }
  }
}
</script>

<style>

</style>
